
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import java.util.ArrayList;
import java.util.List;

import javax.management.ObjectName;

import oxygen.tool.facade.FacadeBenignException;
import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.tool.wlfacade.WLMBSTree;
import oxygen.tool.wlfacade.WLScriptHelper;
import weblogic.management.utils.NameListerMBean;

public class WLSecurity {
  
  protected static void ensureSecurityDomain(WLFacadeContextHelper wlctx) throws Exception {
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    if(!wltree.getDomain().equals("Security")) {
      throw new FacadeBenignException("You need to be in the security domain for this operation");
    }
  } 
  
  public static List securitylister(WLFacadeContextHelper wlctx, String cursor)
    throws Exception {
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return securitylister(wlctx, cursor, (ObjectName)wltree.pointer());
  }
  
  public static List securitylister(WLFacadeContextHelper wlctx, String cursor, ObjectName cmo)
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureAdminServer(wlctx);
    ensureSecurityDomain(wlctx);
    ArrayList al = new ArrayList();
    NameListerMBean mbean = (NameListerMBean)WLScriptHelper.tombean(wlctx, cmo);
    
    while(mbean.haveCurrent(cursor)) {
      al.add(mbean.getCurrentName(cursor));
      mbean.advance(cursor);
    }
    mbean.close(cursor);
    
    return al;
  }
  
}

